package hk.cic.cwrs.bio.integration;




import android.app.Activity;

import java.util.Stack;

/**
 * @author Jake Han
 */
public class ActivityTool {
    private static ActivityTool instance;
    private Stack<Activity> activityStack=new Stack<>();

    public static ActivityTool getInstance() {
        if (instance == null) {
            synchronized (ActivityTool.class) {
                if (instance == null) {
                    instance = new ActivityTool();
                }
            }
        }
        return instance;
    }
    private ActivityTool() {
    }
    // 把一个activity压入栈中
    public void pushOneActivity(Activity actvity) {

        activityStack.add(actvity);
    }

    // 获取栈顶的activity，先进后出原则
    public Activity getLastActivity() {
        return activityStack.lastElement();
    }



    // 移除一个activity
    public void popOneActivity(Activity activity) {
        if (activity != null && activityStack.contains(activity)) {
            activityStack.remove(activity);
            activity.finish();
        }
    }
    /**
     * 结束指定的Activity
     *
     * @param activity Activity
     */
    public void finishActivity(Activity activity) {
        if (activity != null && activityStack.contains(activity)) {
            activityStack.remove(activity);
            activity.finish();
        }
    }

    // 退出所有activity
    public void finishAllActivities() {
        for (int i = activityStack.size() - 1; i >= 0; i--) {

            if (activityStack.get(i) != null) {
                finishActivity(activityStack.get(i));
            }
        }
        activityStack.clear();
    }

    /**
     * 退出应用程序
     */
    public void exitApp() {
        try {
            finishAllActivities();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.exit(0);
        }
    }



}
